package com.winterchen.interceptor;

import com.winterchen.model.Options;
import com.winterchen.model.custom.*;
import com.winterchen.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;



public class HomeResourceInterceptor implements HandlerInterceptor {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TagService tagService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private OptionsService optionsService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private CommentService commentService;



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("HomeResourceInterceptor...preHandle......");
        //导航主要菜单显示
        //分类目录显示
        List<CategoryCustom> categoryList = categoryService.listCategory(1);
        request.setAttribute("categoryList",categoryList);
        //菜单显示
        List<MenuCustom> menuCustomList = menuService.listMenu(1);
        request.setAttribute("menuCustomList",menuCustomList);

        //侧边栏显示

        //标签列表显示
        List<TagCustom> tagList = tagService.listTag(1);
        request.setAttribute("tagList",tagList);
        //获得随机文章
        List<ArticleCustom> randomArticleList = articleService.listRandomArticle(1,8);
        request.setAttribute("randomArticleList",randomArticleList);
        //获得热评文章
        List<ArticleCustom> mostCommentArticleList = articleService.listArticleByCommentCount(1,8);
        request.setAttribute("mostCommentArticleList",mostCommentArticleList);
        //最新评论
        List<CommentListVo> recentCommentList = commentService.listRecentComment(10);
        request.setAttribute("recentCommentList",recentCommentList);

        //获得网站概况
        List<String> siteBasicStatistics = new ArrayList<String>();
        siteBasicStatistics.add(articleService.countArticle(1)+"");
        siteBasicStatistics.add(articleService.countArticleComment(1)+"");
        siteBasicStatistics.add(categoryService.countCategory(1)+"");
        siteBasicStatistics.add(tagService.countTag(1)+"");
        siteBasicStatistics.add(linkService.countLink(1)+"");
        siteBasicStatistics.add(articleService.countArticleView(1)+"");
        request.setAttribute("siteBasicStatistics",siteBasicStatistics);
        //最后更新的文章
        ArticleCustom lastUpdateArticle = articleService.getLastUpdateArticle();
        request.setAttribute("lastUpdateArticle",lastUpdateArticle);

        //页脚显示
        //博客基本信息显示(Options)
        Options options = optionsService.getOptions();
        request.setAttribute("options", options);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}